package com.kt.controller;

import com.alibaba.fastjson.JSON;
import com.kt.domain.Product;
import com.kt.service.imp.ProductServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/nextPage.do")
public class NextPageServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        List<Product> list = null;
        ProductServiceImp productServiceImp = new ProductServiceImp();
        String operate = req.getParameter("p");
        int firstPid = Integer.parseInt(req.getParameter("firstPid"));
        if ("up".equals(operate)) {
            firstPid -= 20;
        }
        if ("down".equals(operate)) {
            firstPid += 20;
        }
        list = productServiceImp.findAllProducts(firstPid);
        if (list == null || list.isEmpty()) {
            resp.getWriter().write("null");
            return;
        }
        resp.getWriter().write(JSON.toJSONString(list));

    }
}
